package com.sixmac.controller;

import com.sixmac.core.annotation.Validator;
import com.sixmac.core.base.model.Format;
import com.sixmac.core.constant.ConstCache;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.cache.CacheKit;
import com.sixmac.core.toolbox.kit.RandomKit;
import com.sixmac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by admin on 2018/6/9.
 */
@Controller
@ResponseBody
public class LoginController extends ApiController {
    @Autowired
    UserService userService;

    /**
     * @api {post} /api/login  通过手机号登陆 （用户登录）
     * @apiName login.login
     * @apiGroup login
     * @apiParam {String}  mobile  手机号码
     * @apiParam {String} captcha 验证码
     * @apiSuccess {String} user.categoryIds 用户关注的分类列表的id
     * @apiSuccess {String} user.headimg 头像
     * @apiSuccess {String} user.id id
     * @apiSuccess {String} user.sign 用户签名
     * @apiSuccess {String} user.token token可以是返回的字符串，也可是${id} 比如 ${1}
     * @apiError {Integer} 1
     */
    @RequestMapping("/login")
    @Validator(name = {"mobile", "captcha"}, format = {Format.REG_PHONE, Format.NOT_NULL})
    public AjaxResult loginByMobile(String mobile, String captcha) {
        return this.userService.login(mobile, captcha);
    }

    /**
     * @api {post} /api/sendCaptcha 发送验证码
     * @apiName login.sendCaptcha
     * @apiGroup login
     * @apiParam {String}  mobile  手机号码
     * @apiSuccess {String} code 验证码
     * @apiError {Integer} 1
     */
    @RequestMapping("/sendCaptcha")
    @Validator(name = {"mobile"}, format = {Format.REG_PHONE})
    public AjaxResult sendCaptcha(String mobile) {
        // 生成验证码
        String code = RandomKit.randomNumbers(6);
        return this.userService.sendCaptcha(mobile, code);
    }

    /**
     * @api {get} /api/verifyCode  验证验证码
     * @apiName login.verifyCode
     * @apiGroup login
     * @apiParam {String}  mobile  手机号码
     * @apiParam {String}  captcha  验证码
     * @apiSuccess {boolean} true
     * @apiError {Integer} false
     */
    @RequestMapping("verifyCode")
    @Validator(name = {"mobile", "captcha"}, format = {Format.REG_PHONE, Format.NOT_NULL})
    public Boolean veriftCaptcha(String mobile, String captcha) {
        String code = CacheKit.get(ConstCache.CAPTCHA, mobile);
        if (captcha != null && captcha.equals(code)) {
            return true;
        }
        return false;

    }


    /**
     * @api {get} /api/otherLogin  第三方登录
     * @apiName login.otherLogin
     * @apiGroup login
     * @apiParam {String}  mobile  手机号码 可以不传，如果没有绑定手机号码必须传入
     * @apiParam {String}  captcha  验证码 可以不传，如果没有绑定手机号码必须传入
     * @apiParam {String}  openId  openId
     * @apiParam {Integer} type 1,qq 2 wechat
     * @apiSuccess {String} user.token token可以是返回的字符串，也可是${id} 比如 ${1}
     * @apiSuccess {boolean} true
     * @apiError {Integer} false
     */
    @RequestMapping("otherLogin")
    @ResponseBody
    @Validator(name = {"openId", "type"}, format = {Format.NOT_NULL, Format.NOT_NULL})
    public AjaxResult otherLogin(String openId, Integer type, String mobile, String captcha) {
        return userService.otherLogin(openId, type, mobile, captcha);
    }


}
